//
//  YLEvaluationResultController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/20.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLEvaluationResultController.h"
#import "KLTNavigationController.h"
#import "YLRiskAssessmentAnswerModel.h"
#import "WHProgressHUD.h"
#import "YLEvaluationResultViewModel.h"
#import "YLRiskAssessmentController.h"
@interface YLEvaluationResultController ()

/**
 测评等级
 */
@property (weak, nonatomic) IBOutlet UILabel *gradeLabel;
/**
 结果介绍
 */
@property (weak, nonatomic) IBOutlet UILabel *remarkLabel;

/**
 剩余次数
 */
@property (weak, nonatomic) IBOutlet UILabel *leftTimesLabel;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end


@interface YLEvaluationResultController()
@property (nonatomic, strong) YLEvaluationResultViewModel *viewModel;
@end

@implementation YLEvaluationResultController


- (YLEvaluationResultViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLEvaluationResultViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    if (_answerModel) {
        _gradeLabel.text = _answerModel.grade;
        _remarkLabel.text = _answerModel.remark;
        _leftTimesLabel.text = [NSString stringWithFormat:@"本月剩余测评%@次", _answerModel.residueEvaluationTimes];
        
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"YLLoadMyInfoNoti" object:nil userInfo:nil];
        
    }

    self.title = @"测评结果";
    
    _scrollView.alwaysBounceVertical = YES;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[UIView new]];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    __weak typeof(self) weakSelf = self;
    [[self.viewModel.requestCommand execute:nil] subscribeNext:^(id  _Nullable x) {
        weakSelf.answerModel = x;
        weakSelf.gradeLabel.text = weakSelf.answerModel.grade;
        weakSelf.remarkLabel.text = weakSelf.answerModel.remark;
        weakSelf.leftTimesLabel.text = [NSString stringWithFormat:@"本月剩余测评%@次", weakSelf.answerModel.residueEvaluationTimes];
    }];
    
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    // 禁用返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
    
    KLTNavigationController *nav = (KLTNavigationController *)self.navigationController;
    nav.panGestureRec.enabled = NO;
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
  
}

// 再次评测
- (IBAction)evaluationAgain:(UIButton *)sender {
    
    if (!_answerModel) {
        return;
    }
    if ([_answerModel.residueEvaluationTimes integerValue] < 1) {
        [WHProgressHUD showError:@"抱歉！您本月测评次数已用完"];
        return;
    }
    
    if (self.navigationController.childViewControllers.count == 2) {
        YLRiskAssessmentController *assessmentController = [[YLRiskAssessmentController alloc] init];
        [self.navigationController pushViewController:assessmentController animated:YES];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }

    
}


// 完成评测
- (IBAction)finshEvaluation:(UIButton *)sender {
//    if (!_answerModel) {
//        return;
//    }
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
    
    KLTNavigationController *nav = (KLTNavigationController *)self.navigationController;
    nav.panGestureRec.enabled = YES;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBackground"] forBarMetrics:UIBarMetricsDefault];
    // 修改标题颜色
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    
    [self.navigationController popToRootViewControllerAnimated:NO];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
